<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    :before-close="closeDialog"
    :close-on-click-modal="false"
    width="50%"
    top="5vh"
  >
    <el-form
      ref="createOrEditForm"
      v-loading="loading"
      label-position="right"
      label-width="100px"
      :model="form"
      :rules="formRules"
    >
      <el-form-item :label="$t('BookStore2[\'Display:Title\']')" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item :label="$t('BookStore2[\'Display:Description\']')" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item :label="$t('BookStore2[\'Display:Content\']')" prop="content">
        <el-input v-model="form.content" type="textarea" />
      </el-form-item>
      <el-form-item :label="$t('BookStore2[\'Display:Type\']')" prop="noticeType">
        <el-select v-model="form.noticeType" :placeholder="$t('BookStore2[\'Display:Type\']')" clearable>
          <el-option v-for="item in dataDict.NoticeType" :key="item.key" :label="$t('BookStore2[\'' + item.key + '\']')" :value="item.key" />
        </el-select>
      </el-form-item>
      <!-- 隐藏表单 -->
      <el-form-item prop="id" hidden>
        <el-input v-model="form.id" />
      </el-form-item>
      <el-form-item prop="concurrencyStamp" hidden>
        <el-input v-model="form.concurrencyStamp" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">{{ $t('BookStore2[\'Cancel\']') }}</el-button>
      <el-button type="primary" @click="submitForm">
        <i class="el-icon-check" />
        {{ $t('BookStore2[\'Save\']') }}
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  getById,
  create,
  edit
} from '@/api/notice/notice'
import { getDataDict } from '@/api/data-dict/data-dict'
export default {
  props: {
    context: {
      type: Object,
      required: true
    },
    visible: {
      type: Boolean,
      required: true,
      default: false
    }
  },
  data() {
    return {
      title: '',
      actionMode: '',
      loading: false,
      dataDict: {},
      form: {
        id: '',
        title: '',
        description: '',
        content: '',
        noticeType: '',
        concurrencyStamp: ''
      },
      formRules: {
        title: [{ required: true, message: this.$t('BookStore2[\'ThisFieldIsRequired\']'), trigger: 'blur' }],
        content: [{ required: true, message: this.$t('BookStore2[\'ThisFieldIsRequired\']'), trigger: 'blur' }]
      }
    }
  },
  async created() {
    this.dataDict = await getDataDict(['NoticeType'])
  },
  watch: {
    context(newVal, oldVal) {
      this.loading = true
      this.title = newVal.title
      this.actionMode = newVal.actionMode
      this.$nextTick(function() {
        if (this.actionMode === 'edit') {
          /* 编辑模式 */
          if (newVal.id) {
            getById(newVal.id).then(resp => {
              if (resp) {
                this.form.id = resp.id
                this.form.concurrencyStamp = resp.concurrencyStamp
                this.form.title = resp.title
                this.form.description = resp.description
                this.form.content = resp.content
                this.form.noticeType = resp.noticeType
              }
              this.loading = false
            })
          }
        } else {
          /* 创建模式 */
          this.loading = false
        }
      })
    }
  },
  methods: {
    submitForm() {
      this.$refs.createOrEditForm.validate(async(isValid, invalidFields) => {
        if (isValid) {
          if (this.actionMode === 'edit') {
            await edit(this.form.id, this.form)
          } else {
            await create(this.form)
          }
          this.$message({ message: this.$t('BookStore2[\'Ok\']'), type: 'success' })
          this.closeDialog()
          this.$emit('saveSucceed')
        }
      })
    },
    closeDialog() {
      this.resetDialog() // 重置表单
      this.$emit('update:visible', false)
    },
    resetDialog() {
      this.$refs.createOrEditForm.resetFields()
      // console.log(this.form)
    }
  }
}
</script>

<style scoped>
</style>
